package d512a.TAKO;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import d512a.TAKO.helpers.DataBaseHelper;
import d512a.TAKO.helpers.DateHelper;

public class ProductTabWidgetActivity extends TabActivity {
	private DataBaseHelper foodDB;
	public static Boolean fromScan = true;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.productinfo);

		handleDatabase();

		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; // Resusable TabSpec for each tab
		Intent intent; // Reusable Intent for each tab
		String foodId = "";
		String barcode = "";
		String scanningsID = "";
		String recID = "";

		// Create an Intent to launch an Activity for the tab (to be reused)
		barcode = getIntent().getExtras().getString("barcode");
		foodId = getIntent().getExtras().getString("foodId");
		scanningsID = getIntent().getExtras().getString("scanningsID");
		recID = getIntent().getExtras().getString("rec_id");

		String date = DateHelper.getDate();
		Cursor cursor;
		if (barcode != null) {
			ProductTabWidgetActivity.fromScan = true;
			if (foodDB.barcodeExists(barcode, barcode.substring(0, 6))) {
				cursor = foodDB.putScannings(barcode, date);
			} else {
				cursor = foodDB.putScannings("Ukendt produkt", date);
			}
			
			cursor.moveToFirst();
			// get lastly inserted row in Scannings table. Get its _id value
			scanningsID = cursor.getString(0);
			cursor.close();
		}
		if (foodId == null) {
			foodId = getFoodIdFromBarcode(barcode, barcode.substring(0, 6));
		}
		
		//For testing
		//foodId = "0159";
		
		intent = new Intent().setClass(this, ProductInfoBasicActivity.class);
		intent.putExtra("foodId", foodId);
		intent.putExtra("scanningsID", scanningsID);
		intent.putExtra("rec_id", recID);
		
		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("product_info_basic")
				.setIndicator("Info", res.getDrawable(R.drawable.tab_product_info))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, ProductInfoDetailedActivity.class);
		intent.putExtra("foodId", foodId);
		intent.putExtra("scanningsID", scanningsID);
		//intent.putExtra("fromScan", fromScan);

		spec = tabHost.newTabSpec("product_info_detailed")
				.setIndicator("Indhold", res.getDrawable(R.drawable.tab_product_mereinfo))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, ProductInfoVitamins.class);
		intent.putExtra("foodId", foodId);
		intent.putExtra("scanningsID", scanningsID);

		spec = tabHost.newTabSpec("product_info_vitamins")
				.setIndicator("Vitaminer", res.getDrawable(R.drawable.tab_product_vitaminer))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, ProductInfoMinerals.class);
		intent.putExtra("foodId", foodId);
		intent.putExtra("scanningsID", scanningsID);

		spec = tabHost.newTabSpec("product_info_minerals")
				.setIndicator("Mineraler", res.getDrawable(R.drawable.tab_product_mineraler))
				.setContent(intent);
		tabHost.addTab(spec);
		
		for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
	    {
	        TabWidget widget = tabHost.getTabWidget();
	        widget.getChildAt(i);
	        final TextView tv = (TextView) widget.getChildAt(i).findViewById(android.R.id.title);        
	        tv.setTextColor(this.getResources().getColorStateList(R.drawable.text_tab_indicator));
	    }
		
		tabHost.setCurrentTab(0);
	}

	private void handleDatabase() {
		foodDB = new DataBaseHelper(this);

		try {
			foodDB.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		} finally {
			foodDB.close();
		}
	}

	private String getFoodIdFromBarcode(String barcode, String shortBarcode) {
		Cursor cursor = foodDB.getFoodId(barcode, shortBarcode);
		String foodId = "";
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			foodId = cursor.getString(0);
		}
		cursor.close();
		return foodId;
	}
}
